// 同步执行写法：window.chrome.webview.hostObjects.sync.{name}


// 异步执行写法：window.chrome.webview.hostObjects.{name};
const client = (window as any)?.chrome?.webview;

const AppFtefScrmHost: any = client?.hostObjects?.ftefscrm;
/**
 * 当前是否在客户端中运行
 */
export const IS_RUN_ON_CLIENT = Boolean(AppFtefScrmHost);

/**
 * 和设备断开连接
 */
export async function exeUnlinkDevice(): Promise<any> {
  return AppFtefScrmHost.unLinkDevice();
}

/**
 * 获取当前连接状态
 * @returns
 */
export async function getIsOnlink(): Promise<boolean> {
  const isLink = await AppFtefScrmHost.isClientLink;
  return isLink;
}
/**
 * 获取服务的Qr连接码
 * @returns
 */
export async function getLinkServerQrCode() {
  const hostBlob = await AppFtefScrmHost.getLinkServerQrCode();

  const Data = await hostBlob.Data;
  const type = await hostBlob.Type;

  return new Blob([new Uint8Array(Data )], {type});
}
/**
 * 执行呼出操作
 * @param phone
 * @returns
 */
export async function exeCallPhone(phone): Promise<string> {
  const isOnlink = await getIsOnlink();
  if (isOnlink) {
    return AppFtefScrmHost.callPhone(phone);
  }
  throw new Error("您的设备未联机，请先进行联机操作");
}
export async function exeEndCall(): Promise<any> {
  AppFtefScrmHost.endCall();
}


/**
 * 给EXE客户端发送消息
 * @param message
 */
export function postExeMessage(message: string) {
  client.postMessage(message);
}
/**
 * 监听exe客户端发送过来的消息
 * @param type
 * @param listener
 */
export function addExeEventListener(type: string, listener: Function) {
  client.addEventListener(type, listener);
}
/**
 * 移除exe客户端的监听消息
 * @param type
 * @param listener
 */
export function removeExeEventListener(type: string, listener: Function) {
  client.removeEventListener(type, listener);
}


